﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KEEPER_BRR_13dotNET.DataAccess;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using KP_CommonLibrary.KP_ObjectReferences;
using KP_CommonLibrary;

namespace KEEPER_BRR_13dotNET.BusinessLogic
{
    public class BLL_KPDKHoSoKD : KP_CommonLibrary.KP_Business.KP_BLListBase
    {
        public BLL_KPDKHoSoKD()
            : base("DangKyKD")
        {

        }
        public void InitDependencyBussines()
        {
            BLThanhVien = new BLL_KPCtThanhVien();
            BLDangKyKD = new BLL_KPDDangKyKD();
            BLHoSoCn = new BLL_KPHoSoCn();
            BLCtTPHoSo = new BLL_KPCtTPHoSo();
            BLCtNganhKd = new BLL_KPCtNganhKd();

        }
        public BLL_KPCtThanhVien BLThanhVien { get; set; }
        public BLL_KPDDangKyKD BLDangKyKD { get; set; }
        public BLL_KPHoSoCn BLHoSoCn { get; set; }
        public BLL_KPCtTPHoSo BLCtTPHoSo { get; set; }
        public BLL_KPCtNganhKd BLCtNganhKd { get; set; }

        public void Insert(DataRow hoSoCnRow, DataRow dangKyKDRow, DataTable tblThanhVien, DataTable tblNganhKd, DataTable tblCtTPHoSo)
        {
            try
            {
                BLHoSoCn.DALHoSoCn.OpenConnection();
                BLHoSoCn.DALHoSoCn.BeginTransaction();
                if (hoSoCnRow.RowState == DataRowState.Added || hoSoCnRow.RowState == DataRowState.Detached)
                {
                    hoSoCnRow["Ma_HsCn"] = BLL_KPCommon.GetNextMaHoSoCaNhan();
                    BLHoSoCn.DALHoSoCn.Insert(BLHoSoCn.DALHoSoCn.Transaction, hoSoCnRow);
                }
                if (hoSoCnRow.RowState == DataRowState.Modified)
                {
                    BLHoSoCn.DALHoSoCn.Update(BLHoSoCn.DALHoSoCn.Transaction, hoSoCnRow);
                }
                dangKyKDRow["Stt_Rec"] = BLL_KPCommon.GetNextSttRec();
                dangKyKDRow["Ma_Tt"] = 2;
                if (dangKyKDRow["Ma_LoaiHs"].ToString() == "DK")
                {
                    dangKyKDRow["Ma_Hs"] = BLL_KPCommon.GetNextHoSoSttRec();
                    dangKyKDRow["So_Gp"] = "02A8" + dangKyKDRow["Ma_Hs"].ToString().PadLeft(6, '0');
                }
                dangKyKDRow["Ma_HsCn"] = hoSoCnRow["Ma_HsCn"];
                DataTable tblNewBn = BLL_KPCommon.NextMa_Bn(BLHoSoCn.DALHoSoCn.Transaction);
                int soBn = (int)tblNewBn.Rows[0]["So_Bn_Ht"];
                int dateCreate = (int)tblNewBn.Rows[0]["Nam_Ht"];


                dangKyKDRow["So_Bn"] = string.Format("{0}/{1}-HKD/{2}", soBn, dangKyKDRow["Ma_LoaiHs"], dateCreate);

                BLDangKyKD.DALDangKyKD.Insert(BLHoSoCn.DALHoSoCn.Transaction, dangKyKDRow);
                foreach (DataRow thanhVienRow in tblThanhVien.Rows)
                {

                    thanhVienRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                    BLThanhVien.DALCtThanhVien.Insert(BLHoSoCn.DALHoSoCn.Transaction, thanhVienRow);

                }
                foreach (DataRow ctHoSoRow in tblCtTPHoSo.Rows)
                {

                    ctHoSoRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                    BLCtTPHoSo.DALCtTPHoSo.Insert(BLHoSoCn.DALHoSoCn.Transaction, ctHoSoRow);

                }
                foreach (DataRow nganhKdRow in tblNganhKd.Rows)
                {
                    if (Convert.ToBoolean(nganhKdRow["Choice"]))
                    {
                        nganhKdRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                        BLCtNganhKd.DALCtNganhKd.Insert(BLHoSoCn.DALHoSoCn.Transaction, nganhKdRow);
                    }
                }
                BLHoSoCn.DALHoSoCn.Transaction.Commit();
                BLHoSoCn.DALHoSoCn.Transaction = null;
            }
            catch (Exception ex)
            {
                BLHoSoCn.DALHoSoCn.Transaction.Rollback();
                BLHoSoCn.DALHoSoCn.Transaction = null;
                throw ex;
            }
            finally
            {

                BLHoSoCn.DALHoSoCn.CloseConnection();
            }
        }
        public void Update(DataRow hoSoCnRow, DataRow dangKyKDRow, DataTable tblThanhVien, DataTable tblNganhKd, DataTable tblCtTPHoSo)
        {
            try
            {
                BLHoSoCn.DALHoSoCn.OpenConnection();
                BLHoSoCn.DALHoSoCn.BeginTransaction();
                //Xóa hồ sơ rồi đẩy lại =))
                BLDangKyKD.DALDangKyKD.DeleteHoSo(dangKyKDRow["Stt_Rec"].ToString(), BLHoSoCn.DALHoSoCn.Connection, BLHoSoCn.DALHoSoCn.Transaction);
                
                if (hoSoCnRow.RowState == DataRowState.Modified)
                {
                    BLHoSoCn.DALHoSoCn.Update(BLHoSoCn.DALHoSoCn.Transaction, hoSoCnRow);
                }
                dangKyKDRow["Ma_Tt"] = 2;
                dangKyKDRow["Ma_HsCn"] = hoSoCnRow["Ma_HsCn"];

                BLDangKyKD.DALDangKyKD.Insert(BLHoSoCn.DALHoSoCn.Transaction, dangKyKDRow);
                foreach (DataRow thanhVienRow in tblThanhVien.Rows)
                {

                    thanhVienRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                    BLThanhVien.DALCtThanhVien.Insert(BLHoSoCn.DALHoSoCn.Transaction, thanhVienRow);

                }
                foreach (DataRow ctHoSoRow in tblCtTPHoSo.Rows)
                {

                    ctHoSoRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                    BLCtTPHoSo.DALCtTPHoSo.Insert(BLHoSoCn.DALHoSoCn.Transaction, ctHoSoRow);

                }
                foreach (DataRow nganhKdRow in tblNganhKd.Rows)
                {
                    if (Convert.ToBoolean(nganhKdRow["Choice"]))
                    {
                        nganhKdRow["Stt_Rec"] = dangKyKDRow["Stt_Rec"];
                        BLCtNganhKd.DALCtNganhKd.Insert(BLHoSoCn.DALHoSoCn.Transaction, nganhKdRow);
                    }
                }
                BLHoSoCn.DALHoSoCn.Transaction.Commit();
                BLHoSoCn.DALHoSoCn.Transaction = null;
            }
            catch (Exception ex)
            {
                BLHoSoCn.DALHoSoCn.Transaction.Rollback();
                BLHoSoCn.DALHoSoCn.Transaction = null;
                throw ex;
            }
            finally
            {

                BLHoSoCn.DALHoSoCn.CloseConnection();
            }
        }
        public DataTable GetThanhVienSchema()
        {
            return BLThanhVien.DALCtThanhVien.GetSchema();
        }
        public DataTable GetDangKyKDSchema()
        {
            return BLDangKyKD.DALDangKyKD.GetSchema();
        }
        public DataTable GetHoSoCnSchema()
        {
            return BLHoSoCn.DALHoSoCn.GetSchema();
        }
        public DataTable GetCtTPHoSoSchema()
        {
            return BLCtTPHoSo.DALCtTPHoSo.GetSchema();
        }
        public DataTable GetCtNganhKdSchema()
        {
            return BLCtNganhKd.DALCtNganhKd.GetSchema();
        }
        public DataTable GetCtNganhKdByHoSo(string hoSoRec)
        {
            return BLCtNganhKd.DALCtNganhKd.SelectCtNganhKdByHoSo(hoSoRec);
        }
        public override void Insert(DataRow item)
        {
            //base.Insert(item);
        }

        public DataRow FindHoSoCaNhan(string soCmnd)
        {
            DataTable tblData = BLHoSoCn.DALHoSoCn.FindHoSoCn(soCmnd);
            if (tblData.Rows.Count > 0)
                return tblData.Rows[0];
            return null;
        }
        public DataRow FindHoSoCaNhan(string tenGt_K, string soGt_K)
        {

            DataTable tblData = BLHoSoCn.DALHoSoCn.FindHoSoCn(tenGt_K, soGt_K);
            return tblData.Rows.Count > 0 ? tblData.Rows[0] : null;
        }
        public DataRow FindDangKyKDBySoBn(string so_Bn)
        {
            DataTable tblData = BLDangKyKD.DALDangKyKD.FindDangKyBySoBn(so_Bn);
            return tblData.Rows.Count > 0 ? tblData.Rows[0] : null;
        }
        public DataRow FindDangKyKDBySttRec(string sttRec)
        {
            DataTable tblData = BLDangKyKD.FindDangKyKDBySttRec(sttRec);
            return tblData.Rows.Count > 0 ? tblData.Rows[0] : null;
        }

        public DataRow FindDangKyKdByMaHs(string maHs)
        {
            DataTable tblData = BLDangKyKD.DALDangKyKD.FindDangKyByMaHs(maHs);
            return tblData.Rows.Count > 0 ? tblData.Rows[0] : null;
        }

        public DataRow FindHoSoCnByMa_HsCn(string maHsCn)
        {
            return BLHoSoCn.FindHoSoCnByMa_HsCn(maHsCn);
        }
        public DataTable SelectCtThanhVienBySttRec(string stt_rec)
        {
            return BLThanhVien.DALCtThanhVien.SelectByStt_Rec(stt_rec);
        }
        public DataTable SelectCtTpHoSoBySttRec(string sttRec)
        {
            return BLCtTPHoSo.SelectCtTpHoSoBySttRec(sttRec);
        }
        public bool KiemTraSoCmnd(string so_Cmnd, out string message)
        {
            message = "";
            return BLDangKyKD.DALDangKyKD.KiemTraSoCmnd(so_Cmnd, out message);

        }
        public bool KiemTraGiayToKhac(string ten_GtK, string so_GtK, out string message)
        {
            message = "Test";
            return false;
        }
        public DataTable FindDangKyKdByKeyword(string keyword, int trangthai1, int trangthai2)
        {
            return BLDangKyKD.FindDangKyKdByKeyword(keyword, trangthai1, trangthai2);
        }

        public void DuyetHoSo(string sttRec, int ma_tt, int nextMa_tt, string lyDo, int type)
        {
            BLDangKyKD.DALDangKyKD.DuyetHoSo(sttRec, ma_tt, nextMa_tt, lyDo, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0, type);
        }

        public void KetThucDuyetHs(string sttRec, int ma_tt, int nextMa_tt, string lyDo)
        {
            BLDangKyKD.DALDangKyKD.KetThucDuyetHs(sttRec, ma_tt, nextMa_tt, lyDo, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0);
        }

        public void TraHoSo(string sttRec)
        {
            BLDangKyKD.DALDangKyKD.TraHoSo(sttRec, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0);
        }
        public bool KiemTraTruocKhiCapLai(string soGp, out string message)
        {

            return BLDangKyKD.DALDangKyKD.KiemTraTruocKhiCapLai(soGp, out message);
        }
        public DataTable SelectDmTt()
        {
            BL_KPDmTt blDmTt = new BL_KPDmTt();
            return blDmTt.Select();
        }
        public DataTable SelectNhatKyHsBySttRec(string sttRec)
        {
            BL_KPNhatKyHs _BlNhatKyHs = new BL_KPNhatKyHs();
            return _BlNhatKyHs.DALNhatKyHs.SelectBySttRec(sttRec);

        }

    }
}
